package com.simple.mipc.base;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Messenger;

import java.util.Objects;

import lombok.Getter;

@Getter
public abstract class MessengerConnection implements ServiceConnection {

    private Messenger messenger;

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        if (Objects.isNull(messenger)) {
            messenger = new Messenger(service);
        }
        initConnected(messenger);
    }

    @Override
    public void onBindingDied(ComponentName name) {
        ServiceConnection.super.onBindingDied(name);
    }

    @Override
    public void onNullBinding(ComponentName name) {
        ServiceConnection.super.onNullBinding(name);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        messenger = null;
    }

    public abstract void initConnected(Messenger messenger);
}
